<template>
  <div class="material">
    <el-card shadow="always" :body-style="{ padding: '20px' }">
      <div slot="header">
        <el-upload
          ref="imgUpload"
          class="upload-demo"
          :action="url"
          :headers="uploadHeaders"
          :on-success="uploadSuccess"
          multiple
          :limit="1"
        >
          <el-button size="small" type="primary">点击上传素材</el-button>
          <div slot="tip" class="el-upload__tip">只能上传jpg/png文件，且不超过500kb</div>
        </el-upload>
      </div>
      <!-- card body -->
      <ul class="list">
        <li v-for="item in list" :key="item.id" class="item">
          <el-card shadow="always" :body-style="{ padding: '20px' }">
            <div slot="header">
              <span>创建时间：{{ item.createTime }}</span>
            </div>
            <img :src="item.ossUrl" alt="">
            <div class="btn-holder">
              <el-button type="danger" size="small" @click="remove(item.id)">删除</el-button>
            </div>
          </el-card>
        </li>
      </ul>

      <!-- 分页器 -->
      <div class="pagination-holder">
        <el-pagination
          :current-page="page.start"
          :page-sizes="page.pageSize"
          :page-size="page.limit"
          layout="total, sizes, prev, pager, next, jumper"
          :total="page.total"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </el-card>

  </div>
</template>

<script>
import mixin from '@/mixins/index'
import {
  requestMaterialList,
  deleteMaterial
} from '@/api/content'
export default {
  mixins: [mixin],
  data() {
    return {
      url: '/lejuAdmin/material/uploadFileOssSave',
      list: []
    }
  },
  created() {
    this.getList()
  },
  methods: {
    // 上传图片成功的钩子
    uploadSuccess(res) {
      console.log(res)
      if (res.code === 20000) {
        this.$message({
          type: 'success',
          message: '上传成功'
        })
        this.getList()
        this.$refs.imgUpload.clearFiles()
      }
    },
    // 获取素材列表
    getList() {
      requestMaterialList(this.page).then(res => {
        console.log(res)
        this.list = res.data.rows
        this.page.total = res.data.total
      })
    },
    // 改变每页数量
    handleSizeChange(val) {
      this.page.limit = val
      this.getList()
    },
    // 改变页码
    handleCurrentChange(val) {
      this.page.start = val
      this.getList()
    },
    // 删除素材
    remove(id) {
      const _this = this
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          deleteMaterial(id).then((res) => {
            // console.log(res);
            if (res.code === 20000) {
              this.$message({
                type: 'success',
                message: '删除成功!'
              })
              _this.getList()
            }
          })
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
    }
  }
}
</script>

<style lang="scss" scoped>
.material{
  .list{
    overflow: hidden;
    .item{
      padding: 10px;
      float: left;
      width: 225px;
      height: 417px;

      .el-card{
        margin: 0px;
        height: 100%;
        width: 100%;
        img{
          width: 163px;
          height: 250px;
        }

        .btn-holder{
          float: right;
          margin-right: 10px;
        }
      }
    }
  }
}
</style>
